package;

import game.script.RoleScript;
import game.buff.AttackDefenseScaleBuff;
import game.worlds.GameRole;
import game.buff.AttackScaleBuff;
import hx.geom.Point;
import game.worlds.RoleActionEffect;
import game.worlds.GameEffect;
import game.event.RoleEvent;
import Math;

class Script extends RoleScript {
	static function main() {}

	override function onInit() {
		super.onInit();
		// Source code
		this.role.addEventListener(RoleEvent.FRAME_ENTER, onRoleEvent);
		this.role.addEventListener(RoleEvent.ATTACKED, onAttacked);
		this.role.addEventListener(RoleEvent.FRAME_CHANGE, changeFrame);
	}

	public var yanInt:Int = 2;
	public var yanSound:Int = 2;
	public var slashSpeed:float = 0;
	public var fx:float = 0;
	public var fxsum:float = 0;
	public var OKO:Int = 0;
	public function onRoleEvent(e:RoleEvent):Void {
		if(yanSound != yanInt){
			
			yanSound = yanInt;
			if(yanSound ==4){
				this.role.assets.playSound("buff");
				}
			
		}
		if (role.getGameEffectByName("yanSum") == null) {
			var yan = new GameEffect(this.role, "yanSum");
			yan.x = role.x;
			yan.y = role.y;
			yan.scaleX = role.scaleX;
			yan.scaleY = 1;
			yan.name = "yanSum";
			yan.fps = 0;
			yan.blendMode = "add";
			role.world.displayBox.addChildAt(yan, 0);
		} else {
			if (yanInt > 4) {
				yanInt = 4;
			}
			role.getGameEffectByName("yanSum").goToFrame(yanInt);
			role.getGameEffectByName("yanSum").x = role.x;
			role.getGameEffectByName("yanSum").y = role.y;
		}
		switch (e.action) {
			case "贽殿遮那·连斩":
				if (role.currentFrame == 4 && role.getGameEffectByName("heichang") == null) {
					var efs = new GameEffect(this.role, "heichang");
					efs.x = role.x - 1000;
					efs.y = role.y - 1000;
					efs.scaleX = 50;
					efs.scaleY = 50;
					efs.name = "heichang";
					efs.fps = 23;
					role.world.displayBox.addChildAt(efs, 0);
				}

				if (role.currentFrame == 24 && role.getGameEffectByName("kuoda") == null) {
					var ef = new GameEffect(this.role, "kuoda");
					ef.x = role.x;
					ef.y = role.y;
					ef.scaleX = 1;
					ef.scaleY = 1;
					ef.name = "kuoda";
					ef.fps = 18;
					role.world.displayBox.addChild(ef);
					var diArray = this.role.getEnemys(200);
					for (i in diArray) {
						i.cardFrameTime = 0.8;
						var feng = new GameEffect(this.role, "fengyin");
						feng.name = "fengyin";
						feng.fps = 18;
						feng.x = i.x;
						feng.y = i.y;
						feng.blendMode = "add";
						role.world.displayBox.addChild(feng);
					}
				}

				var heichang = role.getGameEffectByName("heichang");
				if (heichang != null && heichang.currentFrame == 15) {
					heichang.cardFrameTime = 0.1;
				}
			case "葬炎断罪":
				//要求把敌人插地上，敌人动作为起身动画的第一帧，倒地状态，要能挨打。
				if (role.jumping && role.currentFrame == 23) {
					this.role.goToFrame(15);
				}
				if (!role.jumping && role.currentFrame < 24 && role.currentFrame > 14) {
					this.role.goToFrame(24);
				}
				if (role.currentFrame > 13 && role.currentFrame < 23) {
					var diArray = this.role.getEnemys(100);
					for (i in diArray) {
						i.playSkill("倒落");
						i.x = role.x + 10 * role.scaleX;
						i.y = role.y + 20;
						i.cardFrameTime = 0.1;
					}
				}
				if (role.currentFrame == 24) {
					var diArray = this.role.getEnemys(100);
					for (i in diArray) {
						i.playSkill("起身");
						i.cardFrameTime = 0.1;
						i.invincibleTime = 0;
						i.x = role.x + 10 * role.scaleX;
						i.y = role.y + 10;
						i.scaleX = -role.scaleX;
					}
				}
				if (role.currentFrame > 24 && role.currentFrame < 56) {
					var diArray = this.role.getEnemys(100);
					var x = 0;
					for (i in diArray) {
						i.playSkill("起身");
						i.cardFrameTime = 0.1;
						i.invincibleTime = -1;
						i.x = role.x + 10 * role.scaleX;
						i.y = role.y + 10;
						i.scaleX = -role.scaleX;
						x = 1;
					}
					
					if(x==0){
						role.playSkill("葬炎断罪空技能");
						return;
					}
				}
				if (role.currentFrame == 56) {
					var diArray = this.role.getEnemys(100);
					for (i in diArray) {
						i.playSkill("待机");
						i.goToFrame(0);
						i.invincibleTime = -1;
						i.x = role.x + 10 * role.scaleX;
						i.y = role.y - 10;
						i.scaleX = -role.scaleX;
					}
				}
			case "普通攻击":
			case "空斩":
			if(role.currentFrame == 1){
					yanInt+=1;
					if(yanInt>4)
					{yanInt =4;}
					}
				if(role.currentFrame <7){
					for (i in role.getEnemys(60)){
					role.goToFrame(7);
					}
					if(yanInt==4){
							role.x+=3*role.scaleX;
						}
					}
				
				if (yanInt == 4) {
					this.role.cdData.setCD(e.action,0);
					trace("清空CD");
				}
			case "测试动作":
			case "断罪":
				if(OKO == 1){
					role.playSkill("红断罪");
					
				}
			case "红断罪":
			if (role.currentFrame == 1 && role.getGameEffectByName("heichang") == null) {
					var efs = new GameEffect(this.role, "heichang");
					efs.x = role.x - 1000;
					efs.y = role.y - 1000;
					efs.scaleX = 50;
					efs.scaleY = 50;
					efs.name = "heichang";
					efs.fps = 23;
					role.world.displayBox.addChildAt(efs, 0);
				}
				var heichang = role.getGameEffectByName("heichang");
				if (heichang != null && heichang.currentFrame == 15) {
					heichang.cardFrameTime = 0.1;
				}
			case "蓄力冲刺":
			if(role.currentFrame>0 && role.currentFrame<4){
					slashSpeed+=0.1;
					fxsum = 0;
				}
				if(role.currentFrame>2 && role.currentFrame<4){
					if(role.isKeyDown(this.role.keyReceiver.FAST_MOVE)){
					role.goToFrame(0);
					fx=Math.abs(role.x);
					fxsum = 0;
					}else{
					role.goToFrame(4);
					}
				}
				if(role.currentFrame>4 && role.currentFrame<9){
					var diArray = this.role.getEnemys(100);
						for (i in diArray) {
							role.playSkill("冲刺命中");
							}
					role.x = role.x + slashSpeed * role.scaleX * 2;
					if((slashSpeed/6 * role.scaleX>1)^2 > 1){
						role.getGameEffectByName("hq").scaleX = slashSpeed/6 * role.scaleX;
					}
					if(Math.abs(role.x) != fx){
						fxsum = fxsum + Math.abs(Math.abs(role.x)-fx);
						fx = Math.abs(role.x);
					}
					if(fxsum>100){
						fxsum=0;
						var huolu = new GameEffect(this.role, "huolu");
							huolu.x = role.x-90 * role.scaleX;
							huolu.y = role.y-100;
							huolu.scaleX = role.scaleX;
							huolu.scaleY = 1;
							huolu.fps = 18;
							huolu.blendMode = "add";
							role.world.displayBox.addChild(huolu);
					}
					
				}
		}

		if (role.jumping) {
			if (e.action == "普通攻击") {
				this.role.playSkill("降落");
			}
		}
	}

	public function onAttacked(e:RoleEvent) {
		if (e.hurtData.attackDisplay is GameEffect) {
			switch (e.hurtData.attackDisplay.name) {
				case "yanquan":
					var ef = new GameEffect(this.role, "kuoda");
					ef.x = role.x;
					ef.y = role.y - 50;
					ef.scaleX = 1;
					ef.scaleY = 1;
					ef.name = "kuoda";
					ef.fps = 18;
					ef.blendMode = "add";
					role.world.displayBox.addChild(ef);
					role.world.displayBox.addChild(ef);
					role.world.displayBox.addChild(ef);
					role.assets.playSound("不准跑");
					shenpan(e.hurtData.role);
			}
		}
		switch (e.action) {
			case "葬炎断罪":
				if (role.currentFrame == 57) {
					e.hurtData.role.cardFrameTime = 0;
				}
			case "普通攻击":
			case "冲刺命中":
				if(role.currentFrame<17){
					e.hurtData.role.x = role.x  + role.scaleX*55;
					OKO = 1;
				}else{
					OKO = 0;
				
				}
			
		}
	}

	public function changeFrame(e:RoleEvent) {
		var act = e.action;
		switch (act) {
			case "普通攻击":
				if (role.currentFrame == 0) {
					if (role.getGameEffectByName("huoyanbuff")!=null && yanInt!=4){
						role.playSkill("连斩");
					}
					else{
					switch(yanInt){
						case 1:
							role.goToFrame(31);
						case 2:
							role.goToFrame(69);
						case 3:
							role.goToFrame(100);
						case 4:
						role.playSkill("贽殿遮那·连斩");
					}
					}
				}
				if (role.currentFrame == 25 ||role.currentFrame == 59 ||role.currentFrame == 95 ||role.currentFrame == 122) {
					yanInt += 1;
					if (yanInt > 4) {
						yanInt = 4;
					}
				}
				switch(role.currentFrame){
				case 30:
					role.stopSkill();
				case 68:
					role.stopSkill();
				case 98:
					role.stopSkill();
				}
				

			case "贽殿遮那·连斩":
				yanInt = 0;

			case "真红·红莲二段击":
				if (yanInt == 4 && role.currentFrame == 0) {
					role.playSkill("真红·红莲炎刀");
					yanInt = 2;
					return;
				}
				if (role.currentFrame == 1) {
					yanInt += 1;
				}
				if (yanInt == 4) {
					this.role.cdData.setCD(e.action,0);
					trace("清空CD");
				}
			case "真红·红莲炎刀":

			case "真红·横":
				if(role.currentFrame == 1){
					yanInt+=1;
					if(yanInt>=4)
					{yanInt =4;
						this.role.cdData.setCD(e.action, 0);
					}
					}			
					if(yanInt==4 && role.currentFrame == 0){
						role.playSkill("真红·炎螺旋");
					yanInt=2;
					return;
					}
						if (yanInt == 4) {
					this.role.cdData.setCD(e.action,0);
					trace("清空CD");
				}
			case "贽殿遮那·横炎":
				if(role.currentFrame == 1){
					yanInt+=1;
					if(yanInt>4){yanInt =4;}
					}
					if(yanInt==4 && role.currentFrame == 0){
						role.playSkill("剑气");
					yanInt=2;
					return;
					}
				if (yanInt == 4) {
						this.role.cdData.setCD(e.action,0);
						trace("清空CD");
					}
			case "真红·天斩":
				if(role.currentFrame == 1){
					yanInt+=1;
					if(yanInt>=4)
					{yanInt =4;
						this.role.cdData.setCD(e.action, 0);
					}
					}			
					if(yanInt==4 && role.currentFrame == 0){
						role.playSkill("真红莲空斩");
					yanInt=2;
					return;
					}
			if (yanInt == 4) {
					this.role.cdData.setCD(e.action,0);
					trace("清空CD");
				}
			case "真红莲空斩":	
				yanInt=2;
				
			case "真红":
				if(role.currentFrame == 1){
					yanInt+=1;
					if(yanInt>=4)
					{yanInt =4;
						this.role.cdData.setCD(e.action, 0);
					}
					}			
					if(yanInt==4 && role.currentFrame == 0){
						role.playSkill("灼烧强袭");
					yanInt=2;
					return;
					}
if (yanInt == 4) {
					this.role.cdData.setCD(e.action,0);
					trace("清空CD");
				}
			case "真红·逆锋":
				if(role.currentFrame == 1){
					yanInt+=1;
					if(yanInt>=4)
					{yanInt =4;
						this.role.cdData.setCD(e.action, 0);
					}
					}			
					if(yanInt==4 && role.currentFrame == 0){
						role.playSkill("炎");
					yanInt=2;
					return;
					}
if (yanInt == 4) {
					this.role.cdData.setCD(e.action,0);
					trace("清空CD");
				}
			case "贽殿遮那·壤碎":
				if(role.currentFrame == 1){
					yanInt+=1;if(yanInt>4){yanInt =4;}
					}
				if (yanInt == 4) {
					this.role.cdData.setCD(e.action,0);
					trace("清空CD");
				}
				if(yanInt==4 && role.currentFrame == 0){
						yanInt-=1;
						role.playSkill("火焰巨剑1");
						role.goToFrame(0);
					yanInt=2;
					return;
					}
			case "火焰巨剑1":
				if(role.currentFrame == 12 && (this.role.isKeyDown(this.role.keyReceiver.DOWN) || this.role.isKeyDown(this.role.keyReceiver.SKILL_I))){
					yanInt-=1;
					role.playSkill("火焰巨剑2");
				}
			case "闪灼":
				if(role.jumping){
					if(role.currentFrame == 0){
					role.playSkill("空斩");
				}
				
				}
				else{
				if(role.currentFrame == 1){
					yanInt+=1;
					if(yanInt>=4)
					{yanInt =4;
						this.role.cdData.setCD(e.action, 0);
					}
					}			
					if(yanInt==4 && role.currentFrame == 0){
						role.playSkill("火焰强袭");
					yanInt=2;
					return;
					}
					}
if (yanInt == 4) {
					this.role.cdData.setCD(e.action,0);
					trace("清空CD");
				}
			case "天壤的劫火":
				if(role.currentFrame == 1){
					yanInt+=1;if(yanInt>4){yanInt =4;}
					}	
				if (role.jumping) {
					yanInt+=1;if(yanInt>4){yanInt =4;}
					role.playSkill("天壤");
				}
			case "炎之手":
				if(role.currentFrame == 1){
					yanInt+=1;if(yanInt>4){yanInt =4;}
					}
			case "爆炎":
				if(role.currentFrame == 1){
					
					yanInt+=1;if(yanInt>4){yanInt =4;}
					}	
					if (yanInt == 4) {
					this.role.cdData.setCD(e.action,0);
					trace("清空CD");
				}
			case "炎爆":
				if(role.currentFrame == 1){
				
					yanInt+=1;if(yanInt>4){yanInt =4;}
					}
				if (yanInt == 4) {
					this.role.cdData.setCD(e.action,0);
					trace("清空CD");
				}	
			case "瞬步":
			if(role.getGameEffectByName("huoyanbuff")!=null){
					slashSpeed=0;
					if(yanInt>=2 && role.currentFrame == 0){
						role.playSkill("蓄力冲刺");
					yanInt-=2;
					}
					
				}
			case "蓄力冲刺":
				
			case "空斩":
				
				case "飞焰":
				if(role.currentFrame == 1){
					yanInt+=1;if(yanInt>4){yanInt =4;}
					}
				if (yanInt == 4) {
					this.role.cdData.setCD(e.action,0);
					trace("清空CD");
				}
				if(yanInt==4 && role.currentFrame == 0){
						yanInt-=1;
						role.playSkill("强化飞焰");
						role.goToFrame(0);
					yanInt=2;
					return;
					}
				case "飞焰·空":
				if(role.currentFrame == 1){
					yanInt+=1;
					if(yanInt>4)
					{yanInt =4;}
					}
				if(yanInt ==4){
					
					var fy = role.getGameEffectByName("kfy");
					if (fy!=null){
						fy.name = "yanquan";
						yanInt = 0;
					}
				
				
				}
				case "连斩":
				
				if(role.currentFrame == 14){
					yanInt+=1;
					if(yanInt>3)
					{
						yanInt =4;
						role.playSkill("连斩重击");
					}else{
						if (this.role.isKeyDown(this.role.keyReceiver.ATTACK)) {
						role.playSkill("连斩");
						role.goToFrame(0);
						role.data.effort-=15;
					}
					
					}
					}
				case "待机":
				
				
				
		}

		if (!role.jumping) {
			if (role.actionName == "空中攻击") {
				if (yanInt > 0) {
					yanInt -= 1;
					this.role.playSkill("kj落地");
				} else {
					role.stopSkill();
				}
			}
		}
		if (role.actionName == "待机" && role.currentFrame == 30) {
			role.goToFrame(26);
		}
	}

	public function shenpan(enemy:GameRole):Void {
		var buff = new AttackDefenseScaleBuff("封绝", 3);
		enemy.cardFrameTime = 3;
		var effect = new GameEffect(this.role, "fengyin");
		buff.renderDisplay = effect;
		effect.followTarget = enemy;
		effect.followOffsetPoint = new Point(0, -enemy.height / 2 + 10);
		effect.scaleX = effect.scaleY = 1.5;
		effect.liveTime = 0;
		effect.blendMode = "add";
		effect.alpha = 1;
		// 添加BUFF
		enemy.buffData.addBuff(buff);
	}
}
